chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
    	
    	var commonUrl = 'http://timesheetv2.paic.com.cn/timesheet';
    	if(window.location.href.indexOf(commonUrl) == -1){
    		if(confirm('当前不是日报系统页面，是否自动跳转？')){
    			location.href = commonUrl;
    		}
    		return;
    	}
    	
    	var workDate = request.workDate;
    	var needId = '';
    	var needValue = '';
    	var SR = '';
    	
    	// 查询NEED_ID时需要的时间参数，今天到前三个月
    	var date = new Date();
    	var date_end = date.toLocaleDateString();
    	date.setMonth(date.getMonth()-3);
    	var date_start = date.toLocaleDateString();
    	console.log('date_start : '+date_start);
    	console.log('date_end : '+date_end);
    	
    	$.ajax({
			type:'post',
			async: false,
			url:'https://sqms-dpm.paic.com.cn/dpm/needManage.do',
			data:{
				'stTime':date_start.replace('/', '-').replace('/', '-'),
				'edTime':date_end.replace('/', '-').replace('/', '-'),
				'searchOperFlag':'Y',
				'needStateCode':'IMPLEMENTING'
			},
			success:function(data){
				var index = data.indexOf('<table id="needListTable"');
				var nd = data.match(new RegExp(/NEED_[0-9]+ *</));
				if(index == -1 || nd == null){
					if(confirm('PDM系统未登录，是否自动跳转？')){
						openPDM();
					}
				}else{
					needId = nd[0].replace('<','').replace(/\s+/g,"");
					console.log('needId : ' + needId);
				}
			},
			error:function(){
				if(confirm('PDM系统未登录，是否自动跳转？')){
					openPDM();
				}
			}
    	});
    	
    	$.ajax({
			type:'get',
			async: false,
			url:'https://sqms-dpm.paic.com.cn/dpm/analyzeNeed.do?needId='+needId,
			success:function(data){
				var index = data.indexOf('版本号');
				var sr = data.match(new RegExp(/SR_[0-9]+ *</));
				var desc = data.match(new RegExp(/break-all;">.*</));
				if(index == -1 || sr == null || desc == null){
					if(confirm('PDM系统未登录，是否自动跳转？')){
						openPDM();
					}
				}else{
					var date = new Date();
					var fullYear = date.getFullYear();
					var month = date.getMonth() + 1;
					var day = date.getDate();
					var nowDay = fullYear + '-' + month + '-';
					if (day < 10) {
						nowDay += '0' + day;
					} else {
						nowDay += day;
					}
					if (workDate == '') {
						alert('请先选择填报日期');
						return;
					}
					if (workDate == nowDay) {
						if (!confirm('请最后填写当天日报，确认填写吗？')) {
							return;
						}
					}
					
					SR = sr[0].replace('<','').replace(/\s+/g,"");
					needValue = desc[0].replace('break-all;">','').replace('<','');
					console.log('SR : ' + SR);
					console.log('needValue : ' + needValue);
				
					// 填写【日报时间】值
		    		$('#menuContent').contents().find('#workDate').val(workDate);
		    		
		    		// 初始化【服务工时-非项目】栏位
		    		$('#menuContent').contents().find('[id^=per_]').remove();
		    		$('#menuContent').contents().find('img:last').click();
		        
		        	// 填写【任务归属】值
		        	$('#menuContent').contents().find('[id^=select_per_task_]').val('CS_322303');
		        	$('#menuContent').contents().find('[id^=systemWorkTimes][id$=taskOwnerInput]').val('ICMP-RMC:内部控制管理系统');
		        
		        	// 填写【任务归属-财务受益人】值
		        	$('#menuContent').contents().find('[id^=select_customer_]').show();
		        	$('#menuContent').contents().find('[id^=select_per_customer_]').append('<option selected value=sponsor006>内控中心</option>');
		        	$('#menuContent').contents().find('[id^=select_per_customer_] option:eq(0)').attr('selected','true');
		        	$('#menuContent').contents().find('[id^=select_per_customer_]').val('sponsor006');
		        
		        	// 填写【服务目录】值
		        	$('#menuContent').contents().find('[id^=systemWorkTimes][id$=serviceNo]').val('stask018_2016');
		        	$('#menuContent').contents().find('[id^=systemWorkTimes][id$=serviceItems]').val('应用系统运营支持服务-->PER开发服务-->PER开发-->详细设计及编码测试');
		        
		        	// 填写【任务ID】值
		        	$('#menuContent').contents().find('[id^=select_needType_]').show();
		        	$('#menuContent').contents().find('[id^=edit_per_needType_] option:eq(2)').attr('selected','true');
		        	$('#menuContent').contents().find('[id^=edit_per_needType_] option:eq(2)').val('内部需求');
		        	$('#menuContent').contents().find('[id^=edit_per_task_ID_display_]').val(SR);
		        	$('#menuContent').contents().find('[id^=edit_per_task_ID_]').val(needId);
		        	$('#menuContent').contents().find('[id^=edit_per_task_SR_ID_]').val(SR);
		        
		        	// 填写【需求】值
		        	$('#menuContent').contents().find('[id^=edit_per_task_desc_]').val(needValue);
		        	$('#menuContent').contents().find('[id^=edit_per_task_desc_display_]').val(needId+'-'+needValue);
		        
		        	// 填写【详细说明】值
		        	$('#menuContent').contents().find('[id^=textarea_per_task_remark_]').val(needId+'-'+needValue);
		        
		        	// 填写【工时】值
		        	$('#menuContent').contents().find('[id^=edit_per_time_cost_]').val('7.58');
		        	// 填写【工时-总计】值
		        	$('#menuContent').contents().find('#edit_per_time_label').val('7.58');
		        
		        	// 提交
		        	$('#menuContent').contents().find('form[name=TimeSheetForm]').submit();

		        	// 回到录入页面
		        	window.location.reload(true); 
		    	}
			},
			error:function(){
			}
		});

    	function openPDM(){
    		window.open('http://sqms-dpm.paic.com.cn/dpm/');
    	}
    }
    
);